﻿#region Using

using System;
using System.Net.Security;
using System.ServiceModel;
using System.ServiceModel.Security;

#endregion

namespace CommunicationLibrary
{
    public static class FarmersBreakfastTcpBinding
    {
        private static NetTcpBinding binding;

        public static NetTcpBinding Instance
        {
            get
            {
                return binding ?? (binding = CreateFarmersBreakfastTcpBinding());
            }
        }

        private static NetTcpBinding CreateFarmersBreakfastTcpBinding()
        {
            var security = new NetTcpSecurity();

            security.Mode = SecurityMode.None;

            security.Message = new MessageSecurityOverTcp();
            security.Message.AlgorithmSuite = SecurityAlgorithmSuite.Default;
            security.Message.ClientCredentialType = MessageCredentialType.None;

            security.Transport = new TcpTransportSecurity();
            security.Transport.ClientCredentialType = TcpClientCredentialType.None;
            security.Transport.ProtectionLevel = ProtectionLevel.EncryptAndSign;

            binding = new NetTcpBinding();
            binding.Security = security;

            // TODO Remove this, only for debugging!
            binding.CloseTimeout = TimeSpan.FromHours(1);
            binding.OpenTimeout = TimeSpan.FromHours(1);
            binding.ReceiveTimeout = TimeSpan.FromHours(1);
            binding.SendTimeout = TimeSpan.FromHours(1);

            return binding;
        }
    }
}